home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Menus / Low Level / MenuItem.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  4.5 KB  |  239 lines  |  [TEXT/CWIE]

  1. // MenuItem.cp
  2.  
  3. #ifndef MenuItem_h
  4. #include "MenuItem.h"
  5. #endif
  6. #ifndef Assert_h
  7. #include "Assert.h"
  8. #endif
  9.  
  10. bool MenuItem::Enabled() const
  11.   {
  12.     Assert( CanEnableIndividually() );
  13.     return ( (*menu)->enableFlags & (1L << item) ) != 0;
  14.   }
  15.  
  16. void MenuItem::Enable()
  17.   {
  18.     Assert( CanEnableIndividually() );
  19.     if ( !Enabled() )
  20.         EnableItem( menu, item );
  21.     Assert( Enabled() );
  22.   }
  23.  
  24. void MenuItem::Disable()
  25.   {
  26.     Assert( CanEnableIndividually() );
  27.     if ( Enabled() )
  28.         DisableItem( menu, item );
  29.     Assert( !Enabled() );
  30.   }
  31.  
  32. void MenuItem::SetEnabled( bool b )
  33.   {
  34.     Assert( b == !!b );
  35.     if ( b == Enabled() )
  36.         return;
  37.     
  38.     if ( b )
  39.         EnableItem( menu, item );
  40.      else
  41.         DisableItem( menu, item );
  42.  
  43.     Assert( b == Enabled() );
  44.   }
  45.  
  46. void MenuItem::SetText( ConstPString string )
  47.   {
  48.     Assert( string.Length() > 0 );
  49.     if ( string[0] != '-' )
  50.         SetMenuItemText( menu, item, string );
  51.      else
  52.       {
  53.         // We slip in a null character to avoid this item
  54.         // showing up as a divider.
  55.         String255 edited( "\p\0" );
  56.         edited += string;
  57.         SetMenuItemText( menu, item, edited );
  58.       }
  59.   }
  60.  
  61. MenuItem::SpecialEffect MenuItem::Effect() const
  62.   {
  63.     int16 key;
  64.     GetItemCmd( menu, item, &key );
  65.     Assert( 0 <= key && key < maxuint8 );
  66.     return ( key < int16( commandKey ) )
  67.              ? SpecialEffect( key )
  68.              : commandKey;
  69.   }
  70.  
  71. bool MenuItem::CanSetEffect( SpecialEffect effect ) const
  72.   {
  73.     Assert( effect > noEffect && effect < commandKey );
  74.     int16 key;
  75.     GetItemCmd( menu, item, &key );
  76.     Assert( 0 <= key && key < maxuint8 );
  77.     return key == 0 || key == int16( effect );
  78.   }
  79.  
  80. bool MenuItem::CanClearEffect( SpecialEffect effect ) const
  81.   {
  82.     Assert( effect > noEffect && effect < commandKey );
  83.     int16 key;
  84.     GetItemCmd( menu, item, &key );
  85.     Assert( 0 <= key && key < maxuint8 );
  86.     return key == int16( effect );
  87.   }
  88.  
  89. bool MenuItem::CanSetCommandKey() const
  90.   {
  91.     int16 key;
  92.     GetItemCmd( menu, item, &key );
  93.     Assert( 0 <= key && key < maxuint8 );
  94.     return key == 0 || key >= int16( commandKey );
  95.   }
  96.  
  97. void MenuItem::SetEffect( SpecialEffect effect )
  98.   {
  99.     Assert( CanSetEffect( effect ) );
  100.     SetItemCmd( menu, item, int16( effect ) );
  101.   }
  102.  
  103. void MenuItem::ClearEffect( SpecialEffect effect )
  104.   {
  105.     Assert( CanClearEffect( effect ) );
  106.     SetItemCmd( menu, item, 0 );
  107.   }
  108.  
  109. ::Style MenuItem::Style() const
  110.   {
  111.     ::Style result;
  112.     GetItemStyle( menu, item, &result );
  113.     return result;
  114.   }
  115.  
  116. uint8 MenuItem::Mark() const
  117.   {
  118.     if ( HasSubmenu() )
  119.         return noMark;
  120.     
  121.     int16 result;
  122.     GetItemMark( menu, item, &result );
  123.     Assert( 0 <= result && result < maxuint8 );
  124.     return result;
  125.   }
  126.  
  127. void MenuItem::SetMark( uint8 mark )
  128.   {
  129.     Assert( !HasSubmenu() );
  130.     SetItemMark( menu, item, mark );
  131.   }
  132.  
  133. uint8 MenuItem::CommandKey() const
  134.   {
  135.     int16 key;
  136.     GetItemCmd( menu, item, &key );
  137.     Assert( 0 <= key && key < maxuint8 );
  138.     return ( key < int16( commandKey ) ) ? 0 : key;
  139.   }
  140.  
  141. void MenuItem::SetCommandKey( uint8 key )
  142.   {
  143.     Assert( CanSetCommandKey() );
  144.     SetItemCmd( menu, item, key );
  145.   }
  146.  
  147. void MenuItem::RemoveCommandKey()
  148.   {
  149.     Assert( HasCommandKey() );
  150.     SetItemCmd( menu, item, 0 );
  151.   }
  152.  
  153. MenuID MenuItem::Submenu() const
  154.   {
  155.     if ( !HasSubmenu() )
  156.         return 0;
  157.     
  158.     int16 result;
  159.     GetItemMark( menu, item, &result );
  160.     Assert( 0 <= result && result < maxuint8 );
  161.     return MenuID( result );
  162.   }
  163.  
  164. void MenuItem::SetSubmenu( MenuID id )
  165.   {
  166.     Assert( Mark() == noMark );
  167.     Assert( 1 <= id && id <= 235 );    // 236-255 are reserved for DAs
  168.     SetItemMark( menu, item, id );
  169.     SetEffect( submenu );
  170.   }
  171.  
  172. void MenuItem::RemoveSubmenu()
  173.   {
  174.     ClearEffect( submenu );
  175.     SetItemMark( menu, item, 0 );
  176.   }
  177.  
  178. uint8 MenuItem::ScriptCode() const
  179.   {
  180.     if ( !HasScriptCode() )
  181.         return 0;
  182.     
  183.     int16 result;
  184.     GetItemIcon( menu, item, &result );
  185.     Assert( 0 <= result && result < maxuint8 );
  186.     return result;
  187.   }
  188.  
  189. void MenuItem::SetScriptCode( uint8 id )
  190.   {
  191.     Assert( !HasIcon() );
  192.     SetItemIcon( menu, item, id );
  193.     SetEffect( script );
  194.   }
  195.  
  196. void MenuItem::RemoveScriptCode()
  197.   {
  198.     ClearEffect( script );
  199.     SetItemIcon( menu, item, 0 );
  200.   }
  201.  
  202. bool MenuItem::HasIcon() const
  203.   {    
  204.     if ( HasScriptCode() )
  205.         return false;
  206.     
  207.     int16 icon;
  208.     GetItemIcon( menu, item, &icon );
  209.     Assert( 0 <= icon && icon < maxuint8 );
  210.     return icon != 0;
  211.   }
  212.  
  213. int16 MenuItem::Icon() const
  214.   {
  215.     if ( !HasScriptCode() )
  216.         return 0;
  217.     
  218.     int16 icon;
  219.     GetItemIcon( menu, item, &icon );
  220.     Assert( 0 <= icon && icon < maxuint8 );
  221.     return ( icon == 0 )
  222.              ? 0
  223.              : int16( icon ) + 256;
  224.   }
  225.  
  226. void MenuItem::SetIcon( int16 id )
  227.   {
  228.     Assert( !HasScriptCode() );
  229.     Assert( id > 256 );
  230.     Assert( id < 512 );
  231.     SetItemIcon( menu, item, id - 256 );
  232.   }
  233.  
  234. void MenuItem::RemoveIcon()
  235.   {
  236.     Assert( !HasScriptCode() );
  237.     SetItemIcon( menu, item, 0 );
  238.   }
  239.